package com.fessc.develop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fessc.develop.entity.dbvo.SettleTradeList;
import com.fessc.develop.mapper.SettleTradeListMapper;
import com.fessc.develop.service.interfaces.ISettleTradeListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author y
 * @since 2024-06-27
 */
@Service
public class SettleTradeListServiceImpl extends ServiceImpl<SettleTradeListMapper, SettleTradeList> implements ISettleTradeListService {

    @Resource
    private SettleTradeListMapper settleTradeListMapper;

    /**
     *根据业务核对编码，查询跨境付汇订单明细
     *
     * @param settleId 业务核对编码
     * @return  跨境付汇集合
     */
    @Override
    public List<SettleTradeList> getSettleTradeListBySettleId(String settleId) {
        QueryWrapper<SettleTradeList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("settle_id", settleId);
        return this.list(queryWrapper);
    }

    /**
     *根据订单编号，查询跨境付汇订单明细
     *
     * @param orderno 订单号
     * @param subOrderNoList 订单明细编号集合
     * @return 跨境付汇明细集合
     */
    @Override
    public List<SettleTradeList> getSettleTradeListBySubOrderNos(String orderno, List<String> subOrderNoList) {
        /*
        QueryWrapper<SettleTradeList> queryWrapper = new QueryWrapper<>();
        if (BusiUtil.isNull(orderNoList)) {
            return null;
        }
        queryWrapper.in("suborderno", orderNoList); // 使用in查询多个ID
        return this.list(queryWrapper);

         */
        return settleTradeListMapper.getSettleTradeListBySubOrderNos(orderno, subOrderNoList);
    }

    /**
     *根据业务核对编码和状态，更新跨境付汇订单明细交易状态
     *
     * @param settleId 业务核对编码
     * @param serialnumberStatus 跨境付汇状态
     * @return 更新成功失败标志
     */
    @Override
    public Boolean updateSettleTradeListStatusBySettleId(String settleId, String serialnumberStatus) {
        List<SettleTradeList> settleTradeList = getSettleTradeListBySettleId(settleId);
        settleTradeList.forEach(tradeList -> {
            tradeList.setSerialnumberStatus(serialnumberStatus);
        });
        return this.updateBatchById(settleTradeList);
    }

    /**
     *根据订单编号和业务核对编码，更新跨境付汇订单明细交易状态
     *
     * @param settleTradeList 跨境付汇订单明细
     * @param orderNo 订单编号
     * @param settleId 业务核对编码
     * @return 更新成功失败标志
     */
    @Override
    public Boolean updateSettleTradeListStatusByOrderNo(List<SettleTradeList> settleTradeList, String orderNo, String settleId) {
        settleTradeList.forEach(tradeList -> {
            settleTradeListMapper.updateSettleTradeListStatusByOrderno(tradeList.getSerialnumberStatus(), tradeList.getSuborderno(), orderNo);
        });
        return true;
    }

    /**
     *根据订单编号和业务核对编码，更新跨境付汇订单明细交易状态
     *
     * @param settleId 业务核对编码
     * @param serialnumberStatus 跨境付汇状态
     * @return 更新成功失败标志
     */
    @Override
    public Boolean updateSettleTradeListStatus(String settleId, String serialnumberStatus) {
        UpdateWrapper<SettleTradeList> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("serialnumber_status", serialnumberStatus);
        updateWrapper.eq("settle_id", settleId);
        return this.update(updateWrapper);
    }

    /**
     * 批量更新跨境付汇订单明细交易状态
     *
     * @param settleTradeLists 跨境付汇订单明细
     * @return 更新成功失败标志
     */
    @Override
    public Boolean updateSettleTradeListStatusByOrderList(List<SettleTradeList> settleTradeLists) {
        return settleTradeListMapper.UpdateSettleTradeListStatusByOrderList(settleTradeLists);
    }

}
